package design_patterns;

/**
 * 装饰模式
 *
 * @author Fairy
 */
public class DecoratorPattern {
    public static void main(String[] args) {
        ConcreteComponent c = new ConcreteComponent();
        ConcreteDecoratorA d1 = new ConcreteDecoratorA();
        ConcreteDecoratorB d2 = new ConcreteDecoratorB();

        d1.setComponent(c);
        d2.setComponent(d1);
        d2.operation();
    }
}

abstract class Component {
    public abstract void operation();
}

class ConcreteComponent extends Component {

    @Override
    public void operation() {
        System.out.println("具体对象的操作");
    }
}

abstract class Decorator extends Component {
    protected Component component;

    public void setComponent(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        if (component != null) {
            component.operation();
        }
    }
}

class ConcreteDecoratorA extends Decorator {
    //本类独有的功能
    private String addedState;

    @Override
    public void operation() {
        super.operation();
        addedState = "New State";
        System.out.println("具体的装饰对象A的操作");
    }
}

class ConcreteDecoratorB extends Decorator {

    @Override
    public void operation() {
        super.operation();
        AddedBehavior();
        System.out.println("具体的装饰对象B的操作");
    }

    private void AddedBehavior() {
        System.out.println("区别于A的方法");
    }

}